<?php

	namespace org\tekuna\demo\weblog\action;

	use org\tekuna\demo\weblog\manager\CommentManager;

	use \PDOException;
	
	use org\tekuna\demo\weblog\manager\ArticleManager;
	use org\tekuna\demo\weblog\ArticleKeyFilter;
	
	use org\tekuna\framework\request\Request;
	use org\tekuna\framework\response\http\FourOhFourResponse;
	use org\tekuna\framework\action\ActionEvent;
	
	use org\tekuna\plugin\simpletemplate\SimpleTemplateAction;
	
	
	class ShowArticleAction extends SimpleTemplateAction {
		
		private
			$objArticleManager,
			$objCommentManager;
		
		protected
			$article,
			$comments = array();
		
		
		public function executeTemplate(ActionEvent $objActionEvent, Request $objRequest) {
			
			// set the filter for the article key
			$objActionEvent -> setPartFilter('articleKey', new ArticleKeyFilter());
			
			// get the article key param
			$sArticleKey = $objActionEvent -> getUrlPart('articleKey');
			
			// load article from DB
			try {
			
				$this -> article = $this -> objArticleManager -> findFirstWhere('key', $sArticleKey);

				// set a new application title
				$sBasicTitle = $this -> getApplicationContext() -> getAppTitle();
				$this -> getApplicationContext() -> setAppTitle("{$this->article->title} - $sBasicTitle");
				
			}
			catch (PDOException $objException) {
				
				return new FourOhFourResponse($objRequest);
			}
			
			// find comments
			$this -> comments = $this -> objCommentManager -> findCommentsForArticle($this -> article);
		}
		
		public function setArticleManager(ArticleManager $objArticleManager) {
			
			$this -> objArticleManager = $objArticleManager;
		}

		public function setCommentManager(CommentManager $objCommentManager) {
			
			$this -> objCommentManager = $objCommentManager;
		}
	}